package ru.agiledev.akb.client.panel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import ru.agiledev.akb.client.AspectKnowledgeBaseApplication;
import ru.agiledev.akb.client.window.EditPasswordWindow;
import ru.agiledev.akb.client.window.EditProfileWindow;

/**
 * Created by a.kirillov
 * Date: 4/9/11 Time: 10:55 AM
 */
public class HelloPanel extends HorizontalPanel{
    private EditProfileWindow editProfileWindow;
    private EditPasswordWindow editPasswordWindow;

    public HelloPanel(String userName){
        setSpacing(4);
        setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

        Label username = new Label("Здравствуйте, "+userName+"!");
        username.setStyleName("loginPanelText");
        Anchor report = new Anchor("Какие данные я уже ввел?");
        report.addClickHandler(new ReportClickHandler());
        report.setStyleName("loginPanelText");

        Anchor profile = new Anchor("Профиль пользователя");
        profile.addClickHandler(new ProfileClickHandler());
        profile.setStyleName("loginPanelText");

        Anchor pwdReset = new Anchor("Сменить пароль");
        pwdReset.addClickHandler(new PwdResetClickHandler());
        pwdReset.setStyleName("loginPanelText");

        add(username);
        add(report);
        add(profile);
        add(pwdReset);

        Button exitButton = new Button("Выход");
        exitButton.addClickHandler(new ExitClickHandler());
        exitButton.setStyleName("loginPanelText");

        add(exitButton);
    }

    class ReportClickHandler implements ClickHandler{
        @Override
        public void onClick(ClickEvent event) {
            Window.Location.replace(GWT.getModuleBaseURL() + "reportServlet?uid="+AspectKnowledgeBaseApplication.get().getUser().getId());
        }
    }

    class ProfileClickHandler implements ClickHandler{
        @Override
        public void onClick(ClickEvent event) {
            editProfileWindow = new EditProfileWindow(RootPanel.get().getOffsetWidth()/2-185, RootPanel.get().getOffsetHeight()/2-155);
            editProfileWindow.show();
        }
    }

    class ExitClickHandler implements  ClickHandler {
        @Override
        public void onClick(ClickEvent clickEvent) {
            AspectKnowledgeBaseApplication.get().setUser(null);
        }
    }

    class PwdResetClickHandler implements  ClickHandler {
        @Override
        public void onClick(ClickEvent clickEvent) {
            editPasswordWindow = new EditPasswordWindow(RootPanel.get().getOffsetWidth()/2-185, RootPanel.get().getOffsetHeight()/2-113);
            editPasswordWindow.show();
        }
    }
}
